package com.copa.gamestore.module.user.application.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.copa.gamestore.common.response.ResponseVo;
import com.copa.gamestore.module.user.domain.UserAccount;
import com.copa.gamestore.module.user.domain.dto.UserDto;
import com.copa.gamestore.module.user.application.UserApplicationService;
import com.copa.gamestore.module.user.domain.User;
import com.copa.gamestore.module.user.domain.UserRole;
import com.copa.gamestore.module.user.mapper.UserAccountMapper;
import com.copa.gamestore.module.user.mapper.UserRoleMapper;
import com.copa.gamestore.module.user.service.UserAccountService;
import com.copa.gamestore.module.user.service.UserRoleService;
import com.copa.gamestore.module.user.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.ValidationException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static com.copa.gamestore.common.enums.Role.ROLE_USER;

/**
 * @author copa
 * @createDate 2022-09-22 10:28
 * @function
 */
@Service
public class UserApplicationServiceImpl implements UserApplicationService {

    @Resource
    private UserRoleService userRoleService;

    @Resource
    private UserRoleMapper userRoleMapper;

    @Resource
    private UserService userService;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Resource
    private UserAccountService userAccountService;

    @Override
    public List<String> roleNameByUserId(Long userId) {
        return userRoleMapper.listRoleName(userId);
    }

    @Override
    public User findByUsername(String username) {
        return userService.findByUsername(username);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<User> register(UserDto userDto) {
        User user = userService.register(userDto);
        UserRole userRole = new UserRole().setUserId(user.getId()).setRoleId(ROLE_USER.getRoleId());
        userRoleService.add(userRole);
        UserAccount userAccount = new UserAccount().setUserId(user.getId()).setAccount(new BigDecimal(0));
        userAccountService.add(userAccount);
        return ResponseVo.successMsg("注册成功~");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseVo<String> put(UserDto userDto) {
        if (userInfo().getId() != userDto.getId()) {
            throw new ValidationException("非法操作！");
        }
        userService.put(userDto);
        return ResponseVo.successMsg("修改信息成功~");
    }

    @Override
    public User userInfo() {
        return (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    @Override
    public ResponseVo<UserAccount> account() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return ResponseVo.success(userAccountMapper.selectOne(new QueryWrapper<UserAccount>().eq("user_id", user.getId())));
    }

    @Override
    public void takeAccount(BigDecimal money, UserAccount account) {
        userAccountService.takeAccount(money, Objects.isNull(account) ? account().getData() : account);
    }

    @Override
    public UserAccount accountByUserId(Long userId) {
        return Optional.ofNullable(userAccountMapper.selectOne(
                new QueryWrapper<UserAccount>().eq("user_id", userId)))
                .orElseThrow(() -> new ValidationException("非法操作~"));
    }

    @Override
    public ResponseVo<String> resetPassword(String username) {
        userService.resetPassword(username);
        return ResponseVo.success("重置密码成功~");
    }

    @Override
    public ResponseVo<String> recharge(String username, BigDecimal money) {
        User user = userService.findByUsername(username);
        userAccountService.recharge(user.getId(), money);
        return ResponseVo.success("充值成功~");
    }

    @Override
    public ResponseVo<User> get() {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return ResponseVo.success(user);
    }

    @Override
    public ResponseVo<User> getUser(Long id) {
        return ResponseVo.success(userService.findById(id));
    }
}
